package pyf.demo3;

/**
 * 多个线程操作同一个对象
 * 买火车票例子
 */
//问题：出现脏数据，线程不安全
public class ThreadDemo4 implements Runnable{

    private Integer ticketNums=10;

    @Override
    public void run() {

       while (true){
           if(ticketNums<=0){
               break;
           }
           try {
               //模拟延时
               Thread.sleep(200);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"票");
       }
    }

    public static void main(String[] args) {
        ThreadDemo4 threadDemo4 = new ThreadDemo4();
        new Thread(threadDemo4,"小米").start();
        new Thread(threadDemo4,"小明").start();
        new Thread(threadDemo4,"小红").start();
    }

}
